home *** CD-ROM | disk | FTP | other *** search
- #!/bin/perl
- # augment a base file with general values and components of augment directories
- $ENV{'PATH'} = "/usr/bsd:/bin/:/usr/bin:/usr/sbin:/usr/bin/X11";
-
- #
- # get and set necessary locations and variables
- #
-
- $userHome = $ENV{"HOME"} ;
- if ($userHome eq "/") {
- $userHome = "";
- }
- $DT_utilities = $ENV{"DT_utilities"} ;
- chdir($DT_utilities) || die "unable to cd to $DT_utilities\n";
- $DT_xconfirm = "$DT_utilities/DT_xconfirm";
- $success = 1;
- $failure = 0;
- $f = "$DT_utilities/.DT_Version";
- (-f $f ? `cat $f` : 0 ) =~ /(\d+)/;
- $version = $1;
- $DT_head_pattern = "#-- DT_utilities Compliant File ";
- $DT_end_marker = "#-- End of all DT_utilities stuff ----";
-
- ($filename, $dflt_filename, $file_type, $file) = @ARGV;
- exit($failure) unless $file;
-
-
- sub path_report {
- local($msg, $path) = @_;
- local($sh_path);
- $sh_path = $path =~ /^$userHome\/(.*)/ ? "\$HOME/$1" : $path;
- print STDERR "$msg ($sh_path)\n";
- }
-
- #
- # routines to read/delete/append/write mimetype and mailcap files
- #
-
- $same = 0;
- $add = 1;
- $delete = 2;
- $changed = 3;
-
- sub read_file {
- local($filename, $dflt_filename, $file_type) = @_;
- $xfilename = $filename;
- $xfile_type = $file_type;
- $file_change = $same;
- $do_ask = 1;
- if (!open(IN, "$filename")) {
- open(IN, "$dflt_filename") ||
- die "Unable to open $dflt_filename\n";
- $file_change = $file_change | $add;
- $do_ask = 0;
- &path_report("Creating $file_type file", $filename);
- }
- @file = <IN>;
- close(IN);
- }
-
- sub delete_stuff {
- local($front, $back) = @_;
- local($i, $j, $num_del);
- for ($i=0; $i <= $#file; ++$i) {last if $file[$i] =~ /^$front/;}
- return ($failure) unless $i <= $#file;
- while ($file[$i] =~ /^#/) {
- $file_change = $file_change | $delete;
- return ($success) if splice(@file, $i, 1) =~ /^$back/;
- }
- for ($j=$i; $j <= $#file; ++$j) {last if $file[$j] =~ /^$back/;}
- return ($success) unless $j <= $#file;
- splice(@file, $i, $j - $i + 1);
- $file_change = $file_change | $delete;
- return ($success);
- }
-
- sub get_file_version {
- for (@file) {
- next unless /^$DT_head_pattern V. (\d+)/;
- return($1);
- }
- return(0);
- }
-
- sub append_stuff {
- local($filename, $description) = @_;
- return($failure) unless open(IN, "$filename");
- push(@file, "#\n#-- DT_utilities - $description ----\n#\n");
- $file_change = $file_change | $add;
- push(@file, <IN>);
- close(IN);
- return($success);
- }
-
- sub write_file {
- local($do_backup, $act, $to, $msg, $f);
- return($success) unless $file_change;
- $f = "${xfilename}.bak";
- if (! -f $xfilename) {
- $do_backup = 0;
- undef $backup;
- } elsif (-f $f) {
- $do_backup = 0;
- $backup = "$f exists so no backup will be done.\n\n";
- } else {
- $do_backup = 1;
- $backup = "The original file will be also saved as:\n";
- $backup .= "${f}.\n\n";
- }
- if ($file_change == $add) {
- $act = "add";
- $to = "to";
- } elsif ($file_change == $delete) {
- $act = "delete";
- $to = "from";
- } else {
- $act = "change";
- $to = "of";
- }
- if ($do_ask) {
- $msg = "In order for Toolbox related tasks to work properly, ";
- $msg .= "it is necessary to $act a few lines $to:\n";
- $msg .= "${xfilename}.\n\n$backup";
- $msg .= "Should I modify the file?\n\n";
- $msg .= "Note: Without these $xfile_type entries, ";
- $msg .= "certain things will likely not work as intended.";
- $reply = `$DT_xconfirm yesno "$msg"`;
-
- chop($reply);
- return($failure) if $reply eq "No";
- }
-
- # save the original just in case
- system("/usr/bin/cp $xfilename ${xfilename}.bak") if $do_backup;
-
- open(OUT, ">$xfilename") || die "Unable to write $xfilename\n";
- for (@file) {print OUT;}
- return($success);
- }
-
- &read_file($filename, $dflt_filename, $file_type);
- &delete_stuff("#--FCD96_TOOLBOX_Compliant_File----",
- "#--End of TOOLBOX_SPECIFIC_THINGS");
- &delete_stuff("#--TOOLBOX_Compliant_File----",
- "#--End of TOOLBOX_SPECIFIC_THINGS");
- if (&get_file_version() < $version) {
- &delete_stuff($DT_head_pattern, $DT_end_marker);
- push(@file, "\n$DT_head_pattern V. $version ----\n");
- push(@file,
- "# Do not delete the above line or the end line.\n");
- push(@file, "# DT_utlities uses them to know its stuff.\n");
- &append_stuff($file, "main stuff");
- opendir(DIR, ".") || die "Unable to read current directory\n";
- for $dir (readdir(DIR)) {
- next unless -d $dir;
- next if $dir =~ /^\.{1,2}$/;
- $f = "$dir/$file";
- &append_stuff($f, "$dir augment stuff");
- }
- closedir(DIR);
- push(@file, "#\n$DT_end_marker\n");
- }
- &write_file();
- exit($success);
-